package com.example.tuter.fragment;

import android.content.Intent;
import android.view.View;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.tuter.R;
import com.example.tuter.activity.ViewPostActivity;
import com.example.tuter.activity.util.PageResultHandler;
import com.example.tuter.activity.util.PageResultHelper;
import com.example.tuter.adapter.PostAdapter;
import com.example.tuter.entity.Message;
import com.example.tuter.entity.PageResult;
import com.example.tuter.entity.Post;
import com.example.tuter.util.Constant;
import com.example.tuter.util.httpUtil.HttpFunction.CommunityAPI;

public class FragmentRecommendCommunity extends BaseFragment implements PageResultHandler<Post> {

    private static final String TAG = FragmentRecommendCommunity.class.getSimpleName();
    private PageResultHelper<Post> pageResultHelper = new PageResultHelper<Post>();
    private PostAdapter postAdapter;
    private CommunityAPI communityAPI;

    private SwipeRefreshLayout swipeRefreshLayout;
    private FragmentActivity activity;


    @Override
    protected void initData() {
        communityAPI = Constant.r.create(CommunityAPI.class);
    }


    @Override
    protected void bindEvent() {
    //下拉刷新，清空内容，重新获取
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageResultHelper.getFirstPageInRefreshing(FragmentRecommendCommunity.this, swipeRefreshLayout, postAdapter);
            }
        });
        postAdapter.setRecyclerItemClickListener(new PostAdapter.OnRecyclerItemClickListener() {

            @Override
            public void onRecyclerItemClick(int position) {
                Intent intent = new Intent(getActivity(), ViewPostActivity.class);
                intent.putExtra("post", pageResultHelper.get(position));
                startActivity(intent);

            }
        });
    }

    @Override
    protected void initView(View view) {
        activity = getActivity();


        swipeRefreshLayout = activity.findViewById(R.id.recommend_refresh);
        RecyclerView recyclerView = activity.findViewById(R.id.recommend_recycleView);

        postAdapter = new PostAdapter(pageResultHelper.getData(), activity);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(postAdapter);


        pageResultHelper.addPageScroll(this, recyclerView);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_community_recommend;
    }

    @Override
    public retrofit2.Call<Message<PageResult<Post>>> getDataByPage(Integer pageNo) {
        return communityAPI.getRecommend(pageNo,null);
    }

    @Override
    public void updateUI() {
        postAdapter.notifyItemRangeChanged(0, pageResultHelper.size());

    }
}
